﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SNKeyLength keyLength = SNKeyLength.SN16;
            if(textBox2.Text=="1")
                keyLength = SNKeyLength.SN16;
            if (textBox2.Text == "2")
                keyLength = SNKeyLength.SN20;
            if (textBox2.Text == "3")
                keyLength = SNKeyLength.SN24;
            if (textBox2.Text == "4")
                keyLength = SNKeyLength.SN28;
            if (textBox2.Text == "5")
                keyLength = SNKeyLength.SN32;

            textBox1.Text = GetSerialKeyAlphaNumaric(keyLength);
        }
        public static string GetSerialKeyAlphaNumaric(SNKeyLength keyLength)
        {
            string newSerialNumber = "";
            string SerialNumber = Guid.NewGuid().ToString("N").Substring(0, (int)keyLength).ToUpper();
            for (int iCount = 0; iCount < (int)keyLength; iCount += 4)
                newSerialNumber = newSerialNumber + SerialNumber.Substring(iCount, 4) + "-";
            newSerialNumber = newSerialNumber.Substring(0, newSerialNumber.Length - 1);
            return newSerialNumber;
        }


        public enum
        SNKeyLength
        {
            SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
        }
        public enum SNKeyNumLength
        {
            SN4 = 4, SN8 = 8,
            SN12 =
                12
        }
        public static class RandomSNKGenerator
        {
            private static string
            AppendSpecifiedStr(int length, string str, char[] newKey)
            {
                string
                newKeyStr = "";
                int k = 0;
                for (int i = 0; i < length; i++)
                {
                    for
                    (k = i; k < 4 + i; k++)
                    {
                        newKeyStr += newKey[k];
                    }
                    if (k ==
                    length)
                    {
                        break;
                    }
                    else
                    {
                        i = (k) - 1;
                        newKeyStr +=
                        str;
                    }
                }
                return newKeyStr;
            }

            public static string GetSerialKeyAlphaNumaric(SNKeyLength
            keyLength)
            {
                Guid newguid = Guid.NewGuid();
                string randomStr =
                newguid.ToString("N");
                string tracStr = randomStr.Substring(0,
                (int)keyLength);
                tracStr = tracStr.ToUpper();
                char[] newKey =
                tracStr.ToCharArray();
                string newSerialNumber = "";
                switch (keyLength
                )
                {
                    case SNKeyLength.SN16:
                        newSerialNumber = AppendSpecifiedStr(16,
                        "-", newKey);
                        break;
                    case SNKeyLength.SN20:
                        newSerialNumber =
                        AppendSpecifiedStr(20, "-", newKey);
                        break;
                    case
                    SNKeyLength.SN24:
                        newSerialNumber = AppendSpecifiedStr(24, "-",
                        newKey);
                        break;
                    case SNKeyLength.SN28:
                        newSerialNumber =
                        AppendSpecifiedStr(28, "-", newKey);
                        break;
                    case
                    SNKeyLength.SN32:
                        newSerialNumber = AppendSpecifiedStr(32, "-",
                        newKey);
                        break;
                }

                return newSerialNumber;
            }

            public static string GetSerialKeyNumaric(SNKeyNumLength
            keyLength)
            {
                Random rn = new Random();
                double sd =
                Math.Round(rn.NextDouble() * Math.Pow(10, (int)keyLength) + 4);
                return
                sd.ToString().Substring(0, (int)keyLength);
            }
        }
    }
}
